import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: null
  },
  getters: {
    getUser: state => {
      if(!state.user) {
        try {
          state.user = JOSN.parse(sessionStorage.getItem('user'));
        }
        catch (e) {
          return state.user;
        }
      }
      return state.user;
    }
  },
  mutations: {
    setUser: (state, user) => {
      state.user = user;
      if (user) {
        sessionStorage.setItem('user', JSON.stringify(user));
      } else {
        sessionStorage.removeItem('user');
      }
    }
  }
})
